package com.corner.model.cornerservice.helper;

import com.corner.R;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.Attention;
import com.corner.model.cornerservice.domain.Location;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.service.Service;
import com.corner.model.cornerservice.service.exception.BaseException;
import com.corner.model.cornerservice.utils.ServiceQuery;

import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/25，10.05.
 * 描述：
 */
public class AttentionHelper {
    public static Observable<Boolean> getIsAttention(final Long userId, final Location location) {
        return Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                try {
                    ServiceQuery query = new ServiceQuery();
                    query.equal(Attention.USER, userId, ServiceQuery.FOREIGN_KEY);
                    query.equal(Attention.LOCATION, location.getObjectId(), ServiceQuery.FOREIGN_KEY);
                    Map<String, Long> count = Service.getInstance().attentionService.count(query.toString());
                    if (count.get(Service.COUNT) > 0) {
                        subscriber.onNext(true);
                    } else {
                        subscriber.onNext(false);
                    }
                } catch (BaseException e) {
                    e.printStackTrace();
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<List<Attention>> getAttentions(final Long userId, final int skip) {
        return Observable.create(new Observable.OnSubscribe<List<Attention>>() {
            @Override
            public void call(Subscriber<? super List<Attention>> subscriber) {
                try {
                    ServiceQuery query = new ServiceQuery();
                    query.equal(Attention.USER, userId, ServiceQuery.FOREIGN_KEY);
                    query.setLimit(10);
                    query.setLastId(skip);
                    List<Attention> attentions = Service.getInstance().attentionService.list(query.toString());
                    subscriber.onNext(attentions);
                } catch (BaseException e) {
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<Object> cancleAttention(final Long userId, final Location location) {
        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                try {
                    ServiceQuery query = new ServiceQuery();
                    query.equal(Attention.USER, userId, ServiceQuery.FOREIGN_KEY);
                    query.equal(Attention.LOCATION, location.getObjectId(), ServiceQuery.FOREIGN_KEY);
                    List<Attention> attentions = Service.getInstance().attentionService
                            .list(query.toString());
                    if (attentions != null && attentions.size() > 0) {
                        Service.getInstance().attentionService.delete(attentions.get(0).getObjectId());
                    }
                    subscriber.onNext(null);
                } catch (BaseException e) {
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<Object> deleteAttention(final Attention attention) {
        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                try {
                    Service.getInstance().attentionService.delete(attention.getObjectId());
                    subscriber.onNext(null);
                } catch (BaseException e) {
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    public static Observable<Object> attention(final Long userId, final Location location) {
        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                try {
                    Attention attention = new Attention();
                    attention.setLocation(location);
                    attention.setUser(User.getCurrentUser());
                    Service.getInstance().attentionService.save(attention
                    );
                    subscriber.onNext(null);
                } catch (BaseException e) {
                    subscriber.onError(new Throwable(App.application.getString(R.string.request_data_error)));
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());
    }


}
